//
//  SXReadListViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/29.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXReadListViewController.h"
#import "SXReadListCollectionViewCell.h"
#import "UIView+UIView.h"
#import "SXReadDetailViewController.h"

@interface SXReadListViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *readListCollectionView;

@end

@implementation SXReadListViewController

static NSString * const reuseIdentifier = @"Cell";

- (UICollectionView *)readListCollectionView
{
    if (!_readListCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(kScreenWidth / 5 - 4, 60);
        layout.minimumInteritemSpacing = 5;
        layout.minimumLineSpacing = 3;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        CGFloat h = 0;
        
        if (_dataSourceArray.count % 5 == 0) {
            h = _dataSourceArray.count / 5 * 63;
        }
        else
        {
            h = _dataSourceArray.count / 5 * 63 + 63;
        }
        
        _readListCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, h) collectionViewLayout:layout];
        
        UIImageView *backImageView = [[UIImageView alloc] initWithFrame:_readListCollectionView.bounds];
        backImageView.image = [UIImage imageNamed:@"yuedu"];
        [self.view addSubview:backImageView];
        
        _readListCollectionView.dataSource = self;
        _readListCollectionView.delegate = self;
        _readListCollectionView.backgroundColor = [UIColor clearColor];
        [_readListCollectionView registerClass:[SXReadListCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    }
    return _readListCollectionView;
}

- (void)setDataSourceArray:(NSArray *)dataSourceArray
{
    _dataSourceArray = dataSourceArray;
    [self.view addSubview:self.readListCollectionView];
    [self.readListCollectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSourceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SXReadListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell
    SXReadListSonModel *model = self.dataSourceArray[indexPath.item];
    cell.configureModel = model;
    cell.contentView.backgroundColor = [UIColor clearColor];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SXReadListSonModel *model = self.dataSourceArray[indexPath.item];
    SXReadDetailViewController *readDetailVC = [[SXReadDetailViewController alloc] init];
    readDetailVC.tid = model.tid;
    readDetailVC.titleString = model.tname;
    
    [self.allReadVC.navigationController pushViewController:readDetailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
